package com.nce;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;

public class WavePlayerPanel extends JPanel implements ActionListener, WavePlayerListener {
	JButton play = null;
	JButton stop = null;
	//boolean playing = false;
	
	WaveLoader player = null;
	String filename = null;
	
	public WavePlayerPanel(String filename)
	{
		super();
		this.filename = filename;
		setVisible(true);
		play = new JButton("Play");
		play.setEnabled(true);
		play.addActionListener(this);
		stop = new JButton("Stop");
		stop.addActionListener(this);
		stop.setEnabled(false);
		play.setVisible(true);
		stop.setVisible(true);
		this.add(play);
		this.add(stop);
	}
	
	
	public void actionPerformed(ActionEvent e) {
		String buttonText = e.getActionCommand();
		if (buttonText.equals(play.getText()))
		{
			if (player == null)
			{
				player = new WaveLoader(filename);
				player.setListener(this);
			}
			player.start();
		}
		else if (buttonText.equals(stop.getText()))
		{
			if (player!=null && player.isAlive())
				player.interrupt();
		}

	}


	public void playerStarted() {
		play.setEnabled(false);
		stop.setEnabled(true);
		
	}


	public void playerStopped() {
		play.setEnabled(true);
		stop.setEnabled(false);
		player = null;
	}

}
